﻿using LFramework.Data;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace DataServices
{
    public class DaoSiteSearch
    {
        IDBHelper dbhelper = DBFactory.CreateFromConfig("conn");

        public IList<SiteSearchInfo> Search(string keyword)
        {
            List<SiteSearchInfo> result = new List<SiteSearchInfo>();


            string sql = "select newsid ,title from tbNews Where title like '%" + keyword + "%'   ";
            DataTable dt = dbhelper.QueryForDataTable(sql);
            foreach (DataRow dr in dt.Rows)
            {
                result.Add(new SiteSearchInfo()
                {
                    ID = dr["newsid"].ToString(),
                    Title = dr["title"].ToString(),
                    Model = ModelEnum.News
                });
            }



            sql = "select Artistid ,ArtistName from tbArtist Where ArtistName like '%" + keyword + "%'   ";
            dt = dbhelper.QueryForDataTable(sql);
            foreach (DataRow dr in dt.Rows)
            {
                result.Add(new SiteSearchInfo()
                {
                    ID = dr["Artistid"].ToString(),
                    Title = dr["ArtistName"].ToString(),
                    Model = ModelEnum.Artist
                });
            }



            sql = "select id ,Artworkname from tbArtWork Where Artworkname like '%" + keyword + "%'   ";
            dt = dbhelper.QueryForDataTable(sql);
            foreach (DataRow dr in dt.Rows)
            {
                result.Add(new SiteSearchInfo()
                {
                    ID = dr["id"].ToString(),
                    Title = dr["Artworkname"].ToString(),
                    Model = ModelEnum.ArtWork
                });
            }



            sql = "select id ,title from tbArtWorkReview Where title like '%" + keyword + "%'   ";
            dt = dbhelper.QueryForDataTable(sql);
            foreach (DataRow dr in dt.Rows)
            {
                result.Add(new SiteSearchInfo()
                {
                    ID = dr["id"].ToString(),
                    Title = dr["title"].ToString(),
                    Model = ModelEnum.ArtWorkReview
                });
            }


            sql = "select Adviserid ,Advisername from tbAdviser Where Advisername like '%" + keyword + "%'   ";
            dt = dbhelper.QueryForDataTable(sql);
            foreach (DataRow dr in dt.Rows)
            {
                result.Add(new SiteSearchInfo()
                {
                    ID = dr["Adviserid"].ToString(),
                    Title = dr["Advisername"].ToString(),
                    Model = ModelEnum.Adviser
                });
            }


            return result;
        }
    }
}
